
#include"DesignList.h"


MyLinkedList* myLinkedListCreate() {
    MyLinkedList* NewNode = (MyLinkedList*)malloc(sizeof(MyLinkedList));
    NewNode->head = NULL;
    return NewNode;
}

int myLinkedListGet(MyLinkedList* obj, int index) {
    int i = 0;
    struct ListNode* cur = obj->head;
    while (cur)
    {
        if (i == index)
            return cur->val;
        i++;
        cur = cur->next;
    }
    return -1;
}

// 头插入
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
    struct ListNode* NewNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    NewNode->val = val;
    NewNode->next = NULL;

    if (!obj->head)
    {
        obj->head = NewNode;
    }
    else
    {
        struct ListNode* next = obj->head;
        obj->head = NewNode;
        NewNode->next = next;
    }
}

// 尾部插入
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    struct ListNode* NewNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    NewNode->val = val;
    NewNode->next = NULL;
    // 找尾
    struct ListNode* tail = obj->head;
    if (obj->head == NULL)
    {
        obj->head = NewNode;
    }
    else
    {
        while (tail->next)
        {
            tail = tail->next;
        }
        tail->next = NewNode;
    }
}
// 合法下标节点之前加入节点
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
    int len = 0;
    struct ListNode* cur = obj->head;
    while (cur)
    {
        if (len == index)
        {
            if (len == 0)
            {
                myLinkedListAddAtHead(obj, val);
                return;
            }
            else
            {
                struct ListNode* NewNode = (struct ListNode*)malloc(sizeof(struct ListNode));
                NewNode->val = val;
                NewNode->next = NULL;
                struct ListNode* prev = obj->head;
                while (prev->next != cur)
                    prev = prev->next;
                prev->next = NewNode;
                NewNode->next = cur;
            }
        }
        len++;
        cur = cur->next;
    }
    if (len == index)
        myLinkedListAddAtTail(obj, val);

    
}

void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    int i = 0;
    struct ListNode* cur = obj->head;
    while (cur)
    {
        if (i == index)
        {
            if (i == 0)
            {
                obj->head = cur->next;
                free(cur);
                return;
            }
            struct ListNode* prev = obj->head;
            // 只有一个节点 报错
            
            while (prev->next != cur)
            {
                prev = prev->next;
            }
            prev->next = cur->next;
            free(cur);
            return;
        }
        i++;
        cur = cur->next;
    }
}

void myLinkedListFree(MyLinkedList* obj) {
    struct ListNode* cur = obj->head;
    while (cur)
    {
        struct ListNode* next = cur->next;
        free(cur);
        cur = next;
    }
    free(obj);
}

/**
 * Your MyLinkedList struct will be instantiated and called as such:
 * MyLinkedList* obj = myLinkedListCreate();
 * int param_1 = myLinkedListGet(obj, index);

 * myLinkedListAddAtHead(obj, val);

 * myLinkedListAddAtTail(obj, val);

 * myLinkedListAddAtIndex(obj, index, val);

 * myLinkedListDeleteAtIndex(obj, index);

 * myLinkedListFree(obj);
*/


// test
void print(MyLinkedList* obj)
{
    struct ListNode* cur = obj->head;
    while (cur)
    {
        printf("%d->", cur->val);
        cur = cur->next;
    }
    printf("\n");

}